home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 475 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.5 KB  |  100 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: OSTRSTREAM and sizing
  5. Date: 24 Feb 1996 17:15:11 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4glkpb$c8q@engnews1.Eng.Sun.COM>
  9. References: <4glcfq$sfr@venus.roc.csci.csc.com>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. X-Nntp-Posting-Host: taumet.eng.sun.com
  14. Content-Length: 2711
  15. X-Lines: 75
  16. Originator: clamage@taumet
  17.  
  18. In article sfr@venus.roc.csci.csc.com, tottinge@csci.csc.com (Tim Ottinger) writes:
  19.  
  20. >This might be a silly question, but a coworker of mine noticed this, and we've 
  21. >tested it on two compilers with similar results:
  22.  
  23. >    #include <iostream.h>
  24. >    #include <strstream.h>
  25. >    int main (char * argc, char * argv[])
  26. >    {
  27. >       ostrstream str("abcdefg", 4);
  28. >   
  29. >       cout << "Length = " << str.pcount() << endl;
  30. >       cout << "Data   = " << str.str();
  31. >    }
  32.  
  33. >produces the following output on both the GNU and HP C++ compilers
  34. >    Length = 0
  35. >    Data   = abcdefg
  36.  
  37. >Two things were puzzling about this.  
  38.  
  39. >The first was that the length parameter on the c'tor didn't seem 
  40. >to be used for anything, and the second was that I couldn't spot the 
  41. >right way to tell how big the buffer in the strstreambuf is (how many 
  42. >actual characters are in it).
  43.  
  44. In the example code, you create an ostrstream, and pass in a fixed
  45. buffer. You tell the ostrstream that the buffer can hold 4 characters,
  46. including a terminating null, if any. You don't write anything to
  47. the stream. By a quirk of fate, the data area is actually 8 bytes
  48. in length, and contains "abcdefg" plus a null, but the ostrstream
  49. doesn't know that. (You can tell the ostrstream that the buffer begins
  50. with a null-terminated string by setting the "ate" or "app" mode bits,
  51. but in this case you would have a problem since the null comes after
  52. the 4th character.)
  53.  
  54. When you call pcount(), it tells you how many characters have been
  55. written to the stream, which is zero. When you call str(), it just
  56. returns a pointer to the beginning of the buffer. The buffer contains
  57. whatever it contains. In this case it contains "abcdefg" plus a null,
  58. since you did nothing to change that coincidental fact.
  59.  
  60. If instead of "abcdefg" the buffer was an uninitialized auto variable,
  61. you would also get a zero return from pcount(), but when you tried
  62. to print the result of str() there is no telling what would happen.
  63. Typically it would print a bunch of garbage, often followed by a
  64. memory fault.
  65.  
  66. You might find this variation on your program more interesting:
  67.  
  68. #include <strstream.h>
  69. int main ()
  70. {
  71.     char buf[20] = "abcdefg";
  72.     ostrstream str(buf, 20, ios::out|ios::app);
  73.  
  74.     cout << "Length = " << str.pcount() << endl;
  75.     cout << "Data   = " << str.str() << endl;
  76.  
  77.     str << "zyx" << ends; // appends to existing null-terminated data
  78.     cout << "Length = " << str.pcount() << endl;
  79.     cout << "Data   = " << str.str() << endl;
  80.     return 0;
  81. }
  82.  
  83. Result:
  84. Length = 7         -- initially contains 7 characters, null is not part of string
  85. Data   = abcdefg
  86. Length = 11        -- we added 3 characters plus a null (which is part of the string)
  87. Data   = abcdefgzyx
  88.  
  89. ---
  90. Steve Clamage, stephen.clamage@eng.sun.com
  91.  
  92.  
  93.  
  94. [ To submit articles: Try just posting with your newsreader.
  95.               If that fails, use mailto:std-c++@ncar.ucar.edu
  96.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  97.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  98.   Comments? mailto:std-c++-request@ncar.ucar.edu
  99. ]
  100.